﻿using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Web;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.WebUI
{
	/// <summary>
	/// 通用模块的Url管理类。
	/// </summary>
	public class GenericUrlManager
	{
		private static string _applicationPath = null;
		public static string ApplicationPath
		{
			get
			{
				if (_applicationPath == null)
				{
					try
					{
						_applicationPath =HttpContext.Current.Request.ApplicationPath;
						if (_applicationPath == "/")
							_applicationPath = "";
					}
					catch (Exception e) { }
				}
				return _applicationPath.Replace("/_Specials", "").Replace("/_Channels", "").Replace("/Channels", "").Replace("/YELLOWPAGE", "");
			}
			set
			{
				_applicationPath = value;
			}
		}

		/// <summary>
		/// 获取信息资源缩略图生成器的Url。
		/// </summary>
		/// <param name="resource">获取的资源(id、id + "." + type、实际地址)。</param>
		/// <param name="copyName">获取的资源副本名称。</param>
		/// <param name="resourceType">资源的类型。</param>
		/// <param name="defaultUrl">当资源不存在时的默认地址。</param>
		/// <returns>信息资源缩略图生成器的Url。</returns>
		public static string GetInfoResourceThumbnailGeneratorUrl(string resource, string copyName, string resourceType, string defaultUrl)
		{
			string url = GetInfoResourceThumbnailGeneratorUrl(resource, copyName, resourceType);
			if (string.IsNullOrEmpty(url)) url = defaultUrl;
			return url;
		}

		/// <summary>
		/// 获取信息资源缩略图生成器的Url。
		/// </summary>
		/// <param name="resource">获取的资源(id、id + "." + type、实际地址)。</param>
		/// <param name="copyName">获取的资源副本名称。</param>
		/// <param name="resourceType">资源的类型。</param>
		/// <returns>信息资源缩略图生成器的Url。</returns>
		public static string GetInfoResourceThumbnailGeneratorUrl(string resource, string copyName, string resourceType)
		{
			if (string.IsNullOrEmpty(resource)) return null;

			int id = default(int);
			if (int.TryParse(resource, out id))
				return GetInfoResourceThumbnailGeneratorUrl(id, copyName, resourceType);

			var match = Regex.Match(resource, @"^(\d+)+.(\w+)$", RegexOptions.IgnoreCase);
			
			if (!String.IsNullOrEmpty(match .Value))
			{
				id = int.Parse(match.Groups[1].Value);
				if (string.IsNullOrEmpty(resourceType)) resourceType = match.Groups[2].Value;
				return GetInfoResourceThumbnailGeneratorUrl(id, copyName, resourceType);
			}

			return resource;
		}

		/// <summary>
		/// 获取信息资源缩略图生成器的Url。
		/// </summary>
		/// <param name="resource">获取的资源(id、id + "." + type、实际地址)。</param>
		/// <param name="copyName">获取的资源副本名称。</param>
		/// <returns>信息资源缩略图生成器的Url。</returns>
		public static string GetInfoResourceThumbnailGeneratorUrl(string resource, string copyName)
		{
			return GetInfoResourceThumbnailGeneratorUrl(resource, copyName, null);
		}

		/// <summary>
		/// 获取信息资源缩略图生成器的Url。
		/// </summary>
		/// <param name="resourceId">获取的资源ID。</param>
		/// <param name="copyName">获取的资源副本名称。</param>
		/// <param name="resourceType">资源的类型。</param>
		/// <returns>信息资源缩略图生成器的Url。</returns>
		public static string GetInfoResourceThumbnailGeneratorUrl(int resourceId, string copyName, string resourceType)
		{
			try
			{
				return WebResourceCache.GetResourceCacheUrl(resourceId, copyName, resourceType);
			}
			catch (Exception err)
			{
				string url = "/InfoResource/" + resourceId + (string.IsNullOrEmpty(copyName) ? "" : "_" + copyName) + "." + (string.IsNullOrEmpty(resourceType) ? "img" : resourceType);

				try
				{
					const string logFileName = "ResourceCacheLog.txt";
					string path = HttpRuntime.AppDomainAppPath + logFileName;
					lock (path)
					{
						var writer = File.AppendText(path);
						string message = "在生成资源缓存 [ResourceId : " + resourceId + ", CopyName : " + copyName + ", ResourceType : " + resourceType + "] 时发生了错误，生成客户端重写地址：" + url + "。\n" + err.Message;
						message = DateTime.Now + "\n" + message + "\n\n";
						writer.Write(message);
						writer.Close();
						writer.Dispose();
					}
				}
				catch (Exception err1)
				{
				}

				return url;
			}
		}

		/// <summary>
		/// 获取信息资源缩略图生成器的Url。
		/// </summary>
		/// <param name="resourceId">获取的资源ID。</param>
		/// <param name="copyName">获取的资源副本名称。</param>
		/// <returns>信息资源缩略图生成器的Url。</returns>
		public static string GetInfoResourceThumbnailGeneratorUrl(int resourceId, string copyName)
		{
			return GetInfoResourceThumbnailGeneratorUrl(resourceId, copyName, null);
		}

		/// <summary>
		/// 获取信息资源缩略图生成器的Url的客户端脚本代码。
		/// </summary>
		/// <returns>信息资源缩略图生成器的Url的客户端脚本代码。</returns>
		public static string GetInfoResourceThumbnailGeneratorUrl_ClientScript()
		{
			return @"function(resource, copyName, resourceType)
{
	this.GetInfoResourceThumbnailGeneratorUrl = function(resourceId, copyName, resourceType) { return ""/InfoResource/"" + resourceId + (copyName ? ""_"" + copyName : """") + ""."" + (resourceType ? resourceType : ""img""); };
	
	if(!resource) return null;
	
	if (/^\d+$/gi.test(resource))
		return this.GetInfoResourceThumbnailGeneratorUrl(resource, copyName, resourceType);
	
	var match = resource.match(new RegExp(""^(\\d+)\\.(\\w+)$"", ""i""));
	if (match != null)
	{
		id = match[1];
		if (!resourceType) resourceType = match[2];
		return GetInfoResourceThumbnailGeneratorUrl(id, copyName, resourceType);
	}

	return resource;
}";
		}

		/// <summary>
		/// 获取信息资源上传器的Url。
		/// </summary>
		/// <param name="globalId">信息的全局标识。</param>
		/// <param name="infoType">信息的类别。</param>
		/// <param name="filterResourceTypeName">要过滤的资源类别名称（多个请用"|"分开）。</param>
		/// <param name="clientCallbackFunction">上传成功后的回调函数。</param>
		/// <returns>信息资源上传器的Url。</returns>
		public static string GetInfoResourceUploaderUrl(Guid globalId, string infoType, string filterResourceTypeName, string clientCallbackFunction)
		{
			return ApplicationConfiguration.InfoResourceUploaderUrl + "?GlobalId=" + globalId + "&InfoType=" + infoType + "&FilterResourceTypeName=" + filterResourceTypeName + (string.IsNullOrEmpty(clientCallbackFunction) ? "" : "&ClientCallbackFunction=" + clientCallbackFunction);
		}
		/// <summary>
		/// 获取信息资源上传器的Url。
		/// </summary>
		/// <param name="globalId"></param>
		/// <param name="infoType"></param>
		/// <param name="filterResourceTypeName"></param>
		/// <param name="clientCallbackFunction"></param>
		/// <param name="siteId"></param>
		/// <returns></returns>
		public static string GetInfoResourceUploaderUrl(Guid globalId, string infoType, string filterResourceTypeName, string clientCallbackFunction,int? siteId)
		{
			return ApplicationConfiguration.InfoResourceUploaderUrl + "?GlobalId=" + globalId + "&InfoType=" + infoType + "&FilterResourceTypeName=" + filterResourceTypeName + (string.IsNullOrEmpty(clientCallbackFunction) ? "" : "&ClientCallbackFunction=" + clientCallbackFunction) + (siteId.HasValue ? "&SiteId=" + siteId : "");
		}

		/// <summary>
		/// 构建字典关联下拉列表数据生成器的Url。
		/// </summary>
		/// <param name="siteId">字典所属的站点。</param>
		/// <param name="classCode">字典的类别代码。</param>
		/// <param name="valueType">字典的值类型。</param>
		/// <returns>字典关联下拉列表数据生成器的Url。</returns>
		public static string BuildDicCascadingDropDownListDataGeneratorUrl(int? siteId, string classCode, DicValueType valueType)
		{
			return ApplicationConfiguration.DicCascadingDropDownListDataGeneratorUrl + "?siteId=" + siteId + "&ClassCode=" + classCode + "&ValueType=" + valueType + "&ParentValue={0}";
		}

		public static string BuildDicCascadingDropDownListDataGeneratorUrl(int? siteId, string classCode, DicValueType valueType, DicTextType textType)
		{
			return ApplicationConfiguration.DicCascadingDropDownListDataGeneratorUrl + "?siteId=" + siteId + "&ClassCode=" + classCode + "&ValueType=" + valueType + "&TextType=" + textType + "&ParentValue={0}";
		}




		#region Article
		/// <summary>
		/// 文章查询串信息标识字段名。
		/// </summary>
		public const string Article_Query_InfoIdField = "InfoId";
		/// <summary>
		/// 文章查询串文章代码字段名。
		/// </summary>
		public const string Article_Query_ArticleCodeField = "ArticleCode";
		/// <summary>
		/// 文章查询串当前页字段名。
		/// </summary>
		public const string Article_Query_PageField = "Page";
		/// <summary>
		/// 文章查询串模块代码字段名。
		/// </summary>
		public const string Article_Query_ModuleCodeField = "Code";

		public const string Article_Query_YearField = "Year";

		public const string Article_Query_MonthField = "Month";

		/// <summary>
		/// 构建文章内容Url。
		/// </summary>
		/// <param name="moduleId">文章所处模块的ID。</param>
		/// <param name="articleId">文章的ID。</param>
		/// <returns>文章内容Url。</returns>
		public static string BuildArticleDetailUrl(int moduleId, int articleId)
		{
			return BuildArticleDetailUrl(moduleId, articleId, null);
		}
		/// <summary>
		/// 构建文章内容Url。
		/// </summary>
		/// <param name="moduleId"></param>
		/// <param name="articleId"></param>
		/// <param name="productTypeId"></param>
		/// <param name="publicProductId"></param>
		/// <returns></returns>
		public static string BuildArticleDetailUrl(int moduleId, int articleId, int? productTypeId, int? publicProductId)
		{
			return BuildArticleDetailUrl(moduleId, articleId, productTypeId, publicProductId, null);
		}

		/// <summary>
		/// 构建文章内容Url。
		/// </summary>
		/// <param name="moduleId"></param>
		/// <param name="articleId"></param>
		/// <param name="page"></param>
		/// <returns></returns>
		public static string BuildArticleDetailUrl(int moduleId, int articleId, int? page)
		{
			string[] pathCodes = Modules.GetPathCodesById(moduleId);
			return ApplicationPath + (pathCodes != null && pathCodes.Length > 0 ? "/" + pathCodes.Join("/") : "") + "/" + articleId + (page.HasValue ? "_" + page : "") + ".htm";
		}

		public static string BuildArticleDetailUrl(int? channelId,int moduleId, int articleId,  int? page)
		{
			string[] pathCodes = Modules.GetPathCodesById(moduleId);
			var channelCode = channelId.HasValue ? Dictionaries.GetItem(Sites.CurrentSiteId, GenerucCommon.DicClassName_ChannelCategory, channelId.Value).Value : "";
			return ApplicationPath + (pathCodes != null && pathCodes.Length > 0 ? "/" + pathCodes.Join("/") : "") + (!string.IsNullOrEmpty(channelCode) ? "/Channel_" + channelCode : "") + "/" + articleId + (page.HasValue ? "_" + page : "") + ".htm";
		}

		/// <summary>
		/// 构建文章内容Url。
		/// </summary>
		/// <param name="moduleId"></param>
		/// <param name="articleId"></param>
		/// <param name="productTypeId"></param>
		/// <param name="publicProductId"></param>
		/// <param name="page"></param>
		/// <returns></returns>
		public static string BuildArticleDetailUrl(int moduleId, int articleId, int? productTypeId,int? publicProductId, int? page)
		{
			string[] pathCodes = Modules.GetPathCodesById(moduleId);
			return ApplicationPath + (pathCodes != null && pathCodes.Length > 0 ? "/" + pathCodes.Join("/") : "") + (productTypeId.HasValue ? "/Type_" + productTypeId : "") + (publicProductId.HasValue ? "/ProductId_" + publicProductId : "") + "/" + articleId + (page.HasValue ? "_" + page : "") + ".htm";
		}
		/// <summary>
		/// 构建文章内容Url。
		/// </summary>
		/// <param name="siteId"></param>
		/// <param name="moduleId"></param>
		/// <param name="articleId"></param>
		/// <returns></returns>
		public static string BuildArticleItemDetailUrl(int? siteId,int moduleId, int articleId)
		{
			return BuildArticleItemDetailUrl(siteId, moduleId, articleId, null);
		}
		/// <summary>
		/// 构建文章内容Url。
		/// </summary>
		/// <param name="siteId"></param>
		/// <param name="moduleId"></param>
		/// <param name="articleId"></param>
		/// <param name="productTypeId"></param>
		/// <param name="publicProductId"></param>
		/// <returns></returns>
		public static string BuildArticleItemDetailUrl(int? siteId, int moduleId, int articleId, int? productTypeId, int? publicProductId)
		{
			return BuildArticleItemDetailUrl(siteId, moduleId, articleId,productTypeId,publicProductId, null);
		}
		/// <summary>
		/// 构建文章内容Url。
		/// </summary>
		/// <param name="siteId"></param>
		/// <param name="moduleId"></param>
		/// <param name="articleId"></param>
		/// <param name="page"></param>
		/// <returns></returns>
		public static string BuildArticleItemDetailUrl(int? siteId, int moduleId, int articleId, int? page)
		{
			string[] pathCodes = Modules.GetPathCodesById(moduleId);
			string url="";
			if(siteId.HasValue)
			 url +=string.Format(@"http://{0}",Sites.GetItem(siteId.Value).Domains[0].ToString());
			 url+= (pathCodes != null && pathCodes.Length > 0 ? "/" + pathCodes.Join("/") : "") + "/" + articleId + (page.HasValue ? "_" + page : "") + ".htm";
			return url;
		}
		/// <summary>
		/// 构建文章内容Url。
		/// </summary>
		/// <param name="siteId"></param>
		/// <param name="moduleId"></param>
		/// <param name="articleId"></param>
		/// <param name="productTypeId"></param>
		/// <param name="publicProductId"></param>
		/// <param name="page"></param>
		/// <returns></returns>
		public static string BuildArticleItemDetailUrl(int? siteId, int moduleId, int articleId, int? productTypeId, int? publicProductId, int? page)
		{
			string[] pathCodes = Modules.GetPathCodesById(moduleId);
			string url = "";
			if (siteId.HasValue)
				url += string.Format(@"http://{0}", Sites.GetItem(siteId.Value).Domains[0].ToString());
			url += (pathCodes != null && pathCodes.Length > 0 ? "/" + pathCodes.Join("/") : "") +(productTypeId.HasValue ? "/Type_" + productTypeId : "") + (publicProductId.HasValue ? "/ProductId_" + publicProductId : "")+ "/" + articleId + (page.HasValue ? "_" + page : "") + ".htm";
			return url;
		}


		/// <summary>
		/// 构建文章内容Url。
		/// </summary>
		/// <param name="moduleId">文章所处模块的ID。</param>
		/// <param name="articleCode">文章的代码。</param>
		/// <returns>文章内容Url。</returns>
		public static string BuildArticleDetailUrl(int moduleId, string articleCode)
		{
			return BuildArticleDetailUrl(moduleId, articleCode, null);
		}
		/// <summary>
		/// 构建文章内容Url。
		/// </summary>
		/// <param name="moduleId"></param>
		/// <param name="articleCode"></param>
		/// <param name="productTypeId"></param>
		/// <param name="publicProductId"></param>
		/// <returns></returns>
		public static string BuildArticleDetailUrl(int moduleId, string articleCode, int? productTypeId, int? publicProductId)
		{
			return BuildArticleDetailUrl(moduleId, articleCode, productTypeId, publicProductId, null);
		}

		/// <summary>
		/// 构建文章内容Url。
		/// </summary>
		/// <param name="moduleId"></param>
		/// <param name="articleCode"></param>
		/// <param name="page"></param>
		/// <returns></returns>
		public static string BuildArticleDetailUrl(int moduleId, string articleCode, int? page)
		{
			string[] pathCodes = Modules.GetPathCodesById(moduleId);
			return ApplicationPath + (pathCodes != null && pathCodes.Length > 0 ? "/" + pathCodes.Join("/") : "") + "/" + articleCode + (page.HasValue ? "_" + page : "") + ".htm";
		}
		
		/// <summary>
		/// 构建文章内容Url。
		/// </summary>
		/// <param name="moduleId"></param>
		/// <param name="articleCode"></param>
		/// <param name="productTypeId"></param>
		/// <param name="publicProductId"></param>
		/// <param name="page"></param>
		/// <returns></returns>
		public static string BuildArticleDetailUrl(int moduleId, string articleCode, int? productTypeId, int? publicProductId, int? page)
		{
			string[] pathCodes = Modules.GetPathCodesById(moduleId);
			return ApplicationPath + (pathCodes != null && pathCodes.Length > 0 ? "/" + pathCodes.Join("/") : "") + (productTypeId.HasValue ? "/Type_" + productTypeId : "") + (publicProductId.HasValue ? "/ProductId_" + publicProductId : "") + "/" + articleCode + (page.HasValue ? "_" + page : "") + ".htm";
		}

		/// <summary>
		/// 构建文章列表Url。
		/// </summary>
		/// <param name="moduleId">模块的ID。</param>
		/// <param name="pageIndex">当前页码。</param>
		/// <returns>文章列表Url。</returns>
		public static string BuildArticleListUrl(int? moduleId, int? pageIndex)
		{
			string[] pathCodes = null;
			if (moduleId.HasValue) pathCodes = Modules.GetPathCodesById(moduleId.Value);
			return ApplicationPath + (pathCodes != null && pathCodes.Length > 0 ? "/" + pathCodes.Join("/") : "") + "/List" + (pageIndex.HasValue ? "_P" + pageIndex : "") + ".htm";
		}

		public static string BuildArticleListUrl(int? channelId, int? moduleId, int? pageIndex)
		{
			string[] pathCodes = null;
			if (moduleId.HasValue) pathCodes = Modules.GetPathCodesById(moduleId.Value);
			var channelCode = channelId.HasValue ? Dictionaries.GetItem(Sites.CurrentSiteId, GenerucCommon.DicClassName_ChannelCategory, channelId.Value).Value : "";
			return ApplicationPath + (pathCodes != null && pathCodes.Length > 0 ? "/" + pathCodes.Join("/") : "") + (string.IsNullOrEmpty(channelCode) ? "" : "/Channel_" + channelCode) + "/List" + (pageIndex.HasValue ? "_P" + pageIndex : "") + ".htm";
		}


		public static string BuildArticleListUrl(int? moduleId, string keyword, int? pageIndex)
		{
			if (string.IsNullOrEmpty(keyword)) return BuildArticleListUrl(moduleId, pageIndex);
			string[] pathCodes = null;
			if (moduleId.HasValue) pathCodes = Modules.GetPathCodesById(moduleId.Value);
			return ApplicationPath + (pathCodes != null && pathCodes.Length > 0 ? "/" + pathCodes.Join("/") : "") + (string.IsNullOrEmpty(keyword) ? "" : "/Keyword_" + StringExtensions.HtmlEncode(keyword)) + "/List" + (pageIndex.HasValue ? "_P" + pageIndex : "") + ".htm";
		}

		/// <summary>
		/// 构建文章列表Url。
		/// </summary>
		/// <param name="moduleId"></param>
		/// <param name="productTypeId"></param>
		/// <param name="publicProductId"></param>
		/// <param name="pageIndex"></param>
		/// <returns></returns>
		public static string BuildArticleListUrl(int? moduleId, int? productTypeId,int? publicProductId, int? pageIndex)
		{
			string[] pathCodes = null;
			if (moduleId.HasValue) pathCodes = Modules.GetPathCodesById(moduleId.Value);
			return ApplicationPath + (pathCodes != null && pathCodes.Length > 0 ? "/" + pathCodes.Join("/") : "") + (productTypeId.HasValue ? "/Type_" + productTypeId : "") + (publicProductId.HasValue ? "/ProductId_" + publicProductId : "") + "/List" + (pageIndex.HasValue ? "_P" + pageIndex : "") + ".htm";
		}

		/// <summary>
		/// 构建文章列表Url模式。
		/// </summary>
		/// <param name="moduleId">模块的ID。</param>
		/// <returns>文章列表Url模式。</returns>
		public static string BuildArticleListUrlPattern(int? moduleId)
		{
			string[] pathCodes = null;
			if (moduleId.HasValue) pathCodes = Modules.GetPathCodesById(moduleId.Value);
			return ApplicationPath + (pathCodes != null && pathCodes.Length > 0 ? "/" + pathCodes.Join("/") : "") + "/List_P{0}.htm";
		}
		/// <summary>
		/// 构建文章列表Url模式。
		/// </summary>
		/// <param name="moduleId"></param>
		/// <param name="productTypeId"></param>
		/// <param name="publicProductId"></param>
		/// <returns></returns>
		public static string BuildArticleListUrlPattern(int? moduleId, int? productTypeId, int? publicProductId)
		{
			string[] pathCodes = null;
			if (moduleId.HasValue) pathCodes = Modules.GetPathCodesById(moduleId.Value);
			return ApplicationPath + (pathCodes != null && pathCodes.Length > 0 ? "/" + pathCodes.Join("/") : "") + (productTypeId.HasValue ? "/Type_" + productTypeId : "") + (publicProductId.HasValue ? "/ProductId_" + publicProductId : "") + "/List_P{0}.htm";
		}

		/// <summary>
		/// 构建文章内容Url。
		/// </summary>
		/// <param name="moduleCode">文章所处模块的代码。</param>
		/// <param name="articleId">文章的ID。</param>
		/// <returns>文章内容Url。</returns>
		public static string BuildArticleDetailUrl(string moduleCode, int articleId)
		{
			return BuildArticleDetailUrl(moduleCode, articleId, null);
		}
		/// <summary>
		/// 构建文章内容Url。
		/// </summary>
		/// <param name="moduleCode"></param>
		/// <param name="articleId"></param>
		/// <param name="page"></param>
		/// <returns></returns>
		public static string BuildArticleDetailUrl(string moduleCode, int articleId, int? page)
		{
			return BuildArticleDetailUrl(Modules.GetIdentifier(Sites.CurrentSiteId, moduleCode).Value, articleId, page);
		}
		/// <summary>
		/// 构建文章内容Url。
		/// </summary>
		/// <param name="moduleCode"></param>
		/// <param name="articleId"></param>
		/// <param name="productTypeId"></param>
		/// <param name="publicProductId"></param>
		/// <returns></returns>
		public static string BuildArticleDetailUrl(string moduleCode, int articleId, int? productTypeId, int? publicProductId)
		{
			return BuildArticleDetailUrl(moduleCode, articleId, productTypeId, publicProductId, null);
		}
		/// <summary>
		/// 构建文章内容Url。
		/// </summary>
		/// <param name="moduleCode"></param>
		/// <param name="articleId"></param>
		/// <param name="productTypeId"></param>
		/// <param name="publicProductId"></param>
		/// <param name="page"></param>
		/// <returns></returns>
		public static string BuildArticleDetailUrl(string moduleCode, int articleId, int? productTypeId, int? publicProductId, int? page)
		{
			return BuildArticleDetailUrl(Modules.GetIdentifier(Sites.CurrentSiteId, moduleCode).Value, articleId, productTypeId, publicProductId, page);
		}

		/// <summary>
		/// 构建文章内容Url。
		/// </summary>
		/// <param name="moduleCode">文章所处模块的代码。</param>
		/// <param name="articleCode">文章的代码。</param>
		/// <returns>文章内容Url。</returns>
		public static string BuildArticleDetailUrl(string moduleCode, string articleCode)
		{
			return BuildArticleDetailUrl(moduleCode, articleCode, null);
		}
		/// <summary>
		/// 构建文章内容Url。
		/// </summary>
		/// <param name="moduleCode"></param>
		/// <param name="articleCode"></param>
		/// <param name="page"></param>
		/// <returns></returns>
		public static string BuildArticleDetailUrl(string moduleCode, string articleCode, int? page)
		{
			return BuildArticleDetailUrl(Modules.GetIdentifier(Sites.CurrentSiteId, moduleCode).Value, articleCode, page);
		}
		/// <summary>
		/// 构建文章内容Url。
		/// </summary>
		/// <param name="moduleCode"></param>
		/// <param name="articleCode"></param>
		/// <param name="productTypeId"></param>
		/// <param name="publicProductId"></param>
		/// <returns></returns>
		public static string BuildArticleDetailUrl(string moduleCode, string articleCode, int? productTypeId, int? publicProductId)
		{
			return BuildArticleDetailUrl(moduleCode, articleCode, productTypeId, publicProductId, null);
		}
		/// <summary>
		/// 构建文章内容Url。
		/// </summary>
		/// <param name="moduleCode"></param>
		/// <param name="articleCode"></param>
		/// <param name="productTypeId"></param>
		/// <param name="publicProductId"></param>
		/// <param name="page"></param>
		/// <returns></returns>
		public static string BuildArticleDetailUrl(string moduleCode, string articleCode, int? productTypeId, int? publicProductId, int? page)
		{
			return BuildArticleDetailUrl(Modules.GetIdentifier(Sites.CurrentSiteId, moduleCode).Value, articleCode, productTypeId, publicProductId, page);
		}

		/// <summary>
		/// 构建文章列表Url。
		/// </summary>
		/// <param name="moduleCode">模块的代码。</param>
		/// <param name="pageIndex">当前页码。</param>
		/// <returns>文章列表Url。</returns>
		public static string BuildArticleListUrl(string moduleCode, int? pageIndex)
		{
			return BuildArticleListUrl(Modules.GetIdentifier(Sites.CurrentSiteId, moduleCode), pageIndex);
		}

		public static string BuildArticleListUrl(string moduleCode, string keyword, int? pageIndex)
		{
			if (string.IsNullOrEmpty(keyword)) return BuildArticleListUrl(moduleCode, pageIndex);
			return BuildArticleListUrl(Modules.GetIdentifier(Sites.CurrentSiteId, moduleCode), keyword, pageIndex);
		}

		/// <summary>
		/// 构建文章列表Url。
		/// </summary>
		/// <param name="moduleCode"></param>
		/// <param name="productTypeId"></param>
		/// <param name="publicProductId"></param>
		/// <param name="pageIndex"></param>
		/// <returns></returns>
		public static string BuildArticleListUrl(string moduleCode, int? productTypeId, int? publicProductId, int? pageIndex)
		{
			return BuildArticleListUrl(Modules.GetIdentifier(Sites.CurrentSiteId, moduleCode), productTypeId, publicProductId, pageIndex);
		}

		/// <summary>
		/// 构建文章列表Url模式。
		/// </summary>
		/// <param name="moduleCode">模块的代码。</param>
		/// <returns>文章列表Url模式。</returns>
		public static string BuildArticleListUrlPattern(string moduleCode)
		{
			return BuildArticleListUrlPattern(Modules.GetIdentifier(Sites.CurrentSiteId, moduleCode));
		}
		/// <summary>
		/// 构建文章列表Url模式。
		/// </summary>
		/// <param name="moduleCode"></param>
		/// <param name="productTypeId"></param>
		/// <param name="publicProductId"></param>
		/// <returns></returns>
		public static string BuildArticleListUrlPattern(string moduleCode, int? productTypeId, int? publicProductId)
		{
			return BuildArticleListUrlPattern(Modules.GetIdentifier(Sites.CurrentSiteId, moduleCode), productTypeId, publicProductId);
		}

		#endregion




		#region Link
		/// <summary>
		/// 链接查询串当前页字段名。
		/// </summary>
		public const string Link_Query_PageField = "Page";
		/// <summary>
		/// 链接查询串类别代码字段名。
		/// </summary>
		public const string Link_Query_ClassCodeField = "Code";
		/// <summary>
		/// 构建链接列表Url。
		/// </summary>
		/// <param name="classCode">链接类别的代码。</param>
		/// <param name="pageIndex">链接的页码。</param>
		/// <returns>链接列表Url。</returns>
		public static string BuildLinkListUrl(string classCode, int? pageIndex)
		{
			return ApplicationPath + "/" + classCode + "/LinkList" + (pageIndex.HasValue ? "_P" + pageIndex : "") + ".htm";
		}

		/// <summary>
		/// 构建链接列表Url模式。
		/// </summary>
		/// <param name="classCode">链接类别的代码。</param>
		/// <returns>链接列表Url模式。</returns>
		public static string BuildLinkListUrlPattern(string classCode)
		{
			return ApplicationPath + "/" + classCode + "/LinkList_P{0}.htm";
		}
		#endregion



		#region Comment
		/// <summary>
		/// 评论查询串信息类别字段名。
		/// </summary>
		public const string Comment_Query_InfoTypeField = "InfoType";
		/// <summary>
		/// 评论查询串信息标识字段名。
		/// </summary>
		public const string Comment_Query_InfoIdField = "InfoId";
		/// <summary>
		/// 评论查询串当前页字段名。
		/// </summary>
		public const string Comment_Query_PageField = "Page";
		/// <summary>
		/// 构建评论列表Url。
		/// </summary>
		/// <param name="infoType">评论所属信息类别。</param>
		/// <param name="infoId">评论所属信息标识。</param>
		/// <param name="pageIndex">评论列表的页码。</param>
		/// <returns>评论列表Url。</returns>
		public static string BuildCommentListUrl(string infoType, string infoId, int? pageIndex)
		{
			return ApplicationPath + "/Comment/" + infoType + "/" + infoId + "/List" + (pageIndex.HasValue ? "_P" + pageIndex : "") + ".htm";
		}

		public static string BuildCommentListUrl(string infoType,string code, string infoId, int? pageIndex)
		{
			if (infoType == Articles.INFO_TYPE)
			{
				return ApplicationPath + "/Comment/" + infoType + "/" + code + "/" + infoId + "/List" + (pageIndex.HasValue ? "_P" + pageIndex : "") + ".htm";
			}
			return BuildCommentListUrl(infoType, infoId, pageIndex);
		}

		public static string BuildCommentListUrl(string infoType, string code, string infoId,bool isReverse, int? pageIndex)
		{
			if (infoType == Articles.INFO_TYPE)
			{
				return ApplicationPath + "/Comment/" + infoType + "/" + code + "/" + infoId + (isReverse ? "/ReId_1": "") + "/List" + (pageIndex.HasValue ? "_P" + pageIndex : "") + ".htm";
			}
			return BuildCommentListUrl(infoType, infoId, pageIndex);
		}

		/// <summary>
		/// 构建评论列表Url模式。
		/// </summary>
		/// <param name="infoType">评论所属信息类别。</param>
		/// <param name="infoId">评论所属信息标识。</param>
		/// <returns>评论列表Url模式。</returns>
		public static string BuildCommentListUrlPattern(string infoType, string infoId)
		{
			return ApplicationPath + "/Comment/" + infoType + "/" + infoId + "/List_P{0}.htm";
		}
		#endregion




		#region BuildLoginUrl
		/// <summary>
		/// 构建登录Url。
		/// </summary>
		/// <param name="backUrl">登录成功后的返回地址。</param>
		/// <param name="warningType">登录介面警告的类型。</param>
		/// <returns>登录Url。</returns>
		public static string BuildLoginUrl(string backUrl, LoginWarningType warningType)
		{
			string queryString = "";
			if (!string.IsNullOrEmpty(backUrl)) queryString += (string.IsNullOrEmpty(queryString) ? "?" : "&") + "BackUrl=" + HttpUtility.UrlEncode(backUrl);
			if (warningType != LoginWarningType.None) queryString += (string.IsNullOrEmpty(queryString) ? "?" : "&") + "WarningType=" + warningType;
			return ApplicationPath + "/MemberCenter/Login.aspx" + queryString;
		}
		#endregion




		#region Vote
		/// <summary>
		/// 调查查询串视图类型字段。
		/// </summary>
		public const string Vote_Query_ViewTypeField = "ViewType";
		/// <summary>
		/// 调查查询串类别字段。
		/// </summary>
		public const string Vote_Query_ClassCodeField = "Code";
		/// <summary>
		/// 调查查询串调查标识字段。
		/// </summary>
		public const string Vote_Query_VoteIdField = "VoteId";
		/// <summary>
		/// 调查查询串调查页码字段。
		/// </summary>
		public const string Vote_Query_PageField = "Page";

		public const string Vote_Query_ChannelField = "Channel";
		/// <summary>
		/// 构建调查详细内容Url。
		/// </summary>
		/// <param name="classCode">调查的类别代码。</param>
		/// <param name="voteId">调查的标识。</param>
		/// <returns>调查详细内容Url。</returns>
		public static string BuildVoteDetailUrl(string classCode, int voteId)
		{
			return BuildVoteDetailUrl(VoteViewType.VoteForm, classCode, voteId);
		}

		public static string BuildVoteDetailUrl(string classCode, int? channelId, int voteId)
		{
			return BuildVoteDetailUrl(VoteViewType.VoteForm, classCode,channelId, voteId);
		}

		/// <summary>
		/// 构建调查详细内容Url。
		/// </summary>
		/// <param name="viewType">调查的视图类型。</param>
		/// <param name="classCode">调查的类别代码。</param>
		/// <param name="voteId">调查的标识。</param>
		/// <returns>调查详细内容Url。</returns>
		public static string BuildVoteDetailUrl(VoteViewType viewType, string classCode, int voteId)
		{
			return ApplicationPath + "/Vote/" + classCode + "/" + (viewType == VoteViewType.VoteView ? "View_" : "") + voteId + ".htm";
		}

		public static string BuildVoteDetailUrl(VoteViewType viewType, string classCode,int? channelId, int voteId)
		{
			return ApplicationPath + "/Vote/" + classCode + (channelId.HasValue ? "/Channel_" + channelId : "") + "/" + (viewType == VoteViewType.VoteView ? "View_" : "") + voteId + ".htm";
		}

		/// <summary>
		/// 构建调查列表Url。
		/// </summary>
		/// <param name="classCode">调查的类别代码。</param>
		/// <param name="pageIndex">调查的页码。</param>
		/// <returns>调查列表Url。</returns>
		public static string BuildVoteListUrl(string classCode, int? pageIndex)
		{
			return ApplicationPath + "/Vote/" + classCode + "/List" + (pageIndex.HasValue ? "_P" + pageIndex : "") + ".htm";
		}

		public static string BuildVoteListUrl(string classCode, int? channelId, int? pageIndex)
		{
			return ApplicationPath + "/Vote/" + classCode + (channelId.HasValue ? "/Channel_" + channelId : "") + "/List" + (pageIndex.HasValue ? "_P" + pageIndex : "") + ".htm";
		}

		/// <summary>
		/// 构建调查列表Url模式。
		/// </summary>
		/// <param name="classCode">调查的类别代码。</param>
		/// <returns>调查列表Url模式。</returns>
		public static string BuildVoteListUrlPattern(string classCode)
		{
			return ApplicationPath + "/Vote/" + classCode + "/List_P{0}.htm";
		}

		public static string BuildVoteListUrlPattern(string classCode, int? channelId)
		{
			return ApplicationPath + "/Vote/" + classCode + (channelId.HasValue ? "/Channel_" + channelId : "") + "/List_P{0}.htm";
		}

		#endregion




		#region Special
		/// <summary>
		/// 专题查询串专题代码字段。
		/// </summary>	
		public const string Special_Query_SpecialCodeField = "SpecialCode";

		public const string Special_Query_ClassCodeCodeField = "Code";

		/// <summary>
		/// 专题查询串页代码字段。
		/// </summary>
		public const string Special_Query_PageCodeField = "PageCode";

		/// <summary>
		/// 专题查询串模块代码字段。
		/// </summary>
		public const string Special_Query_ModuleCodeField = "ModuleCode";

		/// <summary>
		/// 专题查询串页码字段。
		/// </summary>
		public const string Special_Query_PageField = "Page";

		/// <summary>
		/// 专题模块查询串页码字段。
		/// </summary>
		public const string Special_Query_ModulePageField = "Page";

		/// <summary>
		/// 专题查询串信息标识字段。
		/// </summary>
		public const string Special_Query_ModuleInfoIdField = "InfoId";

		/// <summary>
		/// 专题查询串信息内容的分页字段。
		/// </summary>
		public const string Special_Query_ModuleInfoContentPageField = "page";

		/// <summary>
		/// 专题查询串视图类型字段。
		/// </summary>
		public const string Special_Query_ModuleViewField = "View";

		/// <summary>
		/// 构建专题列表Url。
		/// </summary>
		/// <param name="pageIndex">当前页码。</param>
		/// <returns>专题列表Url。</returns>
		public static string BuildSpecialListUrl(int? pageIndex)
		{
			return ApplicationPath + "/Specials/List" + (pageIndex.HasValue ? "_P" + pageIndex : "") + ".htm";
		}
		public static string BuildSpecialListUrl(int? channelId, int? pageIndex)
		{
			return ApplicationPath + "/Specials" + (channelId.HasValue ? "/Channel_" + channelId : "") + "/List" + (pageIndex.HasValue ? "_P" + pageIndex : "") + ".htm";
		}
		/// <summary>
		/// 构建专题列表Url。
		/// </summary>
		/// <param name="classCode"></param>
		/// <param name="pageIndex"></param>
		/// <returns></returns>
		public static string BuildSpecialListUrl(string classCode,int? pageIndex)
		{
			return ApplicationPath + "/Specials" + (string.IsNullOrEmpty(classCode) ? "" : "/C_" + classCode) +"/List" + (pageIndex.HasValue ? "_P" + pageIndex : "") + ".htm";
		}

		public static string BuildSpecialListUrl(string classCode,int? channelId, int? pageIndex)
		{
			return ApplicationPath + "/Specials" + (string.IsNullOrEmpty(classCode) ? "" : "/C_" + classCode) + (channelId.HasValue ? "/Channel_" + channelId : "") + "/List" + (pageIndex.HasValue ? "_P" + pageIndex : "") + ".htm";
		}

		/// <summary>
		/// 构建专题列表Url模式。
		/// </summary>
		/// <returns>专题列表Url模式。</returns>
		public static string BuildSpecialListUrlPattern()
		{
			return ApplicationPath + "/Specials/List_P{0}.htm";
		}
		public static string BuildSpecialListUrlPattern(int? channelId)
		{
			return ApplicationPath + "/Specials/List_P{0}.htm";
		}

		/// <summary>
		/// 构建专题列表Url模式。
		/// </summary>
		/// <param name="classCode"></param>
		/// <returns></returns>
		public static string BuildSpecialListUrlPattern(string classCode)
		{
			return ApplicationPath + "/Specials" + (string.IsNullOrEmpty(classCode) ? "" : "/C_" + classCode) + "/List_P{0}.htm";
		}

		public static string BuildSpecialListUrlPattern(string classCode, int? channelId)
		{
			return ApplicationPath + "/Specials" + (string.IsNullOrEmpty(classCode) ? "" : "/C_" + classCode) + (channelId.HasValue ? "/Channel_" + channelId : "") + "/List_P{0}.htm";
		}

		/// <summary>
		/// 构建专题Url。
		/// </summary>
		/// <param name="specialCode">专题代码。</param>
		/// <returns>专题Url。</returns>
		public static string BuildSpecialUrl(string specialCode)
		{
			return ApplicationPath + "/Specials/" + specialCode + "/";
		}

		public static string BuildSpecialUrl(string specialCode, int? channelId)
		{
			return ApplicationPath + "/Specials/" + specialCode+ (channelId.HasValue ? "/Channel_" + channelId : "") + "/" ;
		}
		/// <summary>
		/// 构建专题Url
		/// </summary>
		/// <param name="siteId"></param>
		/// <param name="specialCode"></param>
		/// <returns></returns>
		public static string BuildSpecialUrl(int? siteId,string specialCode)
		{
			if(!siteId.HasValue)
				return ApplicationPath + "/Specials/" + specialCode + "/";
			return string.Format("http://{0}/Specials/{1}/", Sites.GetItem(siteId.Value).MainDomain, specialCode);
		}

		public static string BuildSpecialUrl(int? siteId, string specialCode, int? channelId)
		{
			if (!siteId.HasValue)
				return ApplicationPath + "/Specials/" + specialCode + (channelId.HasValue ? "/Channel_" + channelId : "") + "/";
			return string.Format("http://{0}/Specials/{1}{2}/", Sites.GetItem(siteId.Value).MainDomain, specialCode, (channelId.HasValue ? "/Channel_" + channelId : ""));
		}


		/// <summary>
		/// 构建专题页Url。
		/// </summary>
		/// <param name="specialCode">专题代码。</param>
		/// <param name="pageCode">专题页代码。</param>
		/// <returns>专题页Url。</returns>
		public static string BuildSpecialPageUrl(string specialCode, string pageCode)
		{
			return BuildSpecialUrl(specialCode) + pageCode + "/";
		}

		/// <summary>
		/// 构建专题模块Url。
		/// </summary>
		/// <param name="specialCode">专题代码。</param>
		/// <param name="pageCode">专题页代码。</param>
		/// <param name="moduleCode">模块代码。</param>
		/// <returns>专题模块Url。</returns>
		public static string BuildSpecialModuleUrl(string specialCode, string pageCode, string moduleCode)
		{
			return BuildSpecialPageUrl(specialCode, pageCode) + moduleCode + "/";
		}

		/// <summary>
		/// 构建专题模块列表Url。
		/// </summary>
		/// <param name="specialCode">专题代码。</param>
		/// <param name="pageCode">专题页代码。</param>
		/// <param name="moduleCode">模块代码。</param>
		/// <param name="pageIndex">列表页码。</param>
		/// <returns>专题模块列表Url。</returns>
		public static string BuildSpecialModuleListUrl(string specialCode, string pageCode, string moduleCode, int? pageIndex)
		{
			return BuildSpecialModuleUrl(specialCode, pageCode, moduleCode) + "List" + (pageIndex.HasValue ? "_P" + pageIndex : "") + ".htm";
		}

		/// <summary>
		/// 构建专题模块列表Url模式。
		/// </summary>
		/// <param name="specialCode">专题代码。</param>
		/// <param name="pageCode">专题页代码。</param>
		/// <param name="moduleCode">模块代码。</param>
		/// <returns>专题模块列表Url模式。</returns>
		public static string BuildSpecialModuleListUrlPattern(string specialCode, string pageCode, string moduleCode)
		{
			return BuildSpecialModuleUrl(specialCode, pageCode, moduleCode) + "List_P{0}.htm";
		}

		/// <summary>
		/// 构建专题模块内容Url。
		/// </summary>
		/// <param name="specialCode">专题代码。</param>
		/// <param name="pageCode">专题页代码。</param>
		/// <param name="moduleCode">模块代码。</param>
		/// <param name="infoId">信息标识。</param>
		/// <returns>专题模块内容Url。</returns>
		public static string BuildSpecialModuleContentUrl(string specialCode, string pageCode, string moduleCode, string infoId)
		{
			return BuildSpecialModuleContentUrl(specialCode, pageCode, moduleCode, infoId, null);
		}

		/// <summary>
		/// 构建专题模块内容Url。
		/// </summary>
		/// <param name="specialCode">专题代码。</param>
		/// <param name="pageCode">专题页代码。</param>
		/// <param name="moduleCode">模块代码。</param>
		/// <param name="infoId">信息标识。</param>
		/// <param name="page">当前页。</param>
		/// <returns>专题模块内容Url。</returns>
		public static string BuildSpecialModuleContentUrl(string specialCode, string pageCode, string moduleCode, string infoId, int? page)
		{
			return BuildSpecialModuleUrl(specialCode, pageCode, moduleCode) + infoId + (page.HasValue ? "_" + page : "") + ".htm";
		}

		/// <summary>
		/// 构建专题模块内容评论Url。
		/// </summary>
		/// <param name="specialCode">专题代码。</param>
		/// <param name="pageCode">专题页代码。</param>
		/// <param name="moduleCode">模块代码。</param>
		/// <param name="infoId">信息标识。</param>
		/// <returns>专题模块内容评论Url。</returns>
		public static string BuildSpecialModuleContentCommentUrl(string specialCode, string pageCode, string moduleCode, string infoId, int? pageIndex)
		{
			return BuildSpecialModuleUrl(specialCode, pageCode, moduleCode) + infoId + "/CommentList" + (pageIndex.HasValue ? "_P" + pageIndex : "") + ".htm";
		}

		/// <summary>
		/// 构建专题模块内容评论Url模式。
		/// </summary>
		/// <param name="specialCode">专题代码。</param>
		/// <param name="pageCode">专题页代码。</param>
		/// <param name="moduleCode">模块代码。</param>
		/// <param name="infoId">信息标识。</param>
		/// <returns>专题模块内容评论Url。</returns>
		public static string BuildSpecialModuleContentCommentUrlPattern(string specialCode, string pageCode, string moduleCode, string infoId)
		{
			return BuildSpecialModuleUrl(specialCode, pageCode, moduleCode) + infoId + "/CommentList_P{0}.htm";
		}
		#endregion
	}

	/// <summary>
	/// 调查视图类型。
	/// </summary>
	public enum VoteViewType
	{
		/// <summary>
		/// 调查表单。
		/// </summary>
		VoteForm,
		/// <summary>
		/// 调查结果。
		/// </summary>
		VoteView
	}
}